//
// Created By: Xiaofeng.Gu 2021/05/18
//

#pragma once

#include "../DefinesCore.hpp"
#include <string>
#include "nlohmann/json.hpp"
#include "cybertron/core/ConcurrentQueue.hpp"
#include <thread>

CYBERTRON_BEGIN

class AlarmClient
{
public:
	enum AlarmCategory
	{
		eUndefinedAlarm = 0,
		eNodeAlarm,
		eBusinessAlarm,
		ePod
	};

	enum AlarmLevel
	{
		eUndefinedLevel = 0,
		eHigh,
		eMiddle,
		eLow
	};

	AlarmClient();

	void Initialize(unsigned int connectMaxSeconds = 30, long alarmQueueMaxCount = 1000);
	bool PushAlarmMessage(nlohmann::json& messageBody);
	void Destroy();

protected:
	void Create(const std::string& ip, const std::string& port, unsigned int connectMaxSeconds = 30, long alarmQueueMaxCount = 1000);
	void Run();
	bool SendAlarmMessage(const std::string& url, nlohmann::json& messageBody, std::string& result, std::string& error);
	ConcurrentQueue<nlohmann::json> mAlarmQueue;

private:
	std::string mAlarmServerIp;
	std::string mAlarmServerPort;
	unsigned int mConnectMaxSeconds;
	long mAlarmQueueMaxCount;
	bool mInitialized;
	std::thread mRunningThread;
	bool mStop;
};

CybertronCoreAPI extern std::shared_ptr<AlarmClient> gAlarmClient;
CybertronCoreAPI void AlarmSetup(
	unsigned int connectMaxSeconds = 30, long alarmQueueMaxCount = 1000);

CybertronCoreAPI bool AlarmSendMessage(
	AlarmClient::AlarmLevel level,
	int errorCode,
	const std::string& nodeType,
	const std::string& nodeId,
	const std::string& alarmDescription);

CybertronCoreAPI void GetAlarmServerConfig();
CYBERTRON_END